import 'package:flutter/material.dart';
import 'package:flutter_app/RandomWordsWidget.dart';
import 'StateManager.dart';
import 'package:flutter_app/NewRoute.dart';
import 'package:flutter_app/CupertinoTestRoute.dart';
import 'package:flutter_app/baseWidgets/TextAndTypeRoute.dart';
import 'package:flutter_app/baseWidgets/SwitchAndCheckBoxTestRoute.dart';
import 'package:flutter_app/baseWidgets/TextFieldAndFromRoute.dart';
import 'package:flutter_app/layoutWidgets/LayoutWidgetTestRoute.dart';
import 'package:flutter_app/containerWidgets/ContainerWidgetsTestRoute.dart';
import 'package:flutter_app/scrollWidgets/ScrollWidgetsRoute.dart';
import 'package:flutter_app/featuresWidgets/FeaturesWidgesTestRoute.dart';
import 'package:flutter_app/eventHandlingAndNotification/EventHandlingAndNotificationTestRoute.dart';
import 'package:flutter_app/animations/AnimationsTestRoute.dart';
import 'package:flutter_app/customWidgets/CustomWisgetsTestRoute.dart';
import 'package:flutter_app/fileAndNetwork/FileAndNetworkTestRoute.dart';
import 'package:flutter_app/jspangstudydemo/JspangStudyDemoRoute.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      //标题
      title: 'Flutter Demo',
      theme: new ThemeData(
          //主题色设置
          primaryColor: Colors.blue),
      //注册路由表
      routes: {
        "new_page": (context) => NewRoute(),
      },
      home: new MyHomePage(
        title: 'Flutter Demo Home Page',
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  State<StatefulWidget> createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0; //计数

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.only(top: 20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Row(
                textDirection: TextDirection.rtl,
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  Image(
                    //asset中加载图片
                    image: AssetImage('assets/images/avatar.png'),
                    width: 100.0,
                  ),
                  Image(
                    //网络加载图片
                    image: NetworkImage(
                        "https://avatars2.githubusercontent.com/u/20411648?s=460&v=4"),
                    width: 100.0,
                  ),
                ],
              ),
              Text('You have pushed the button this many times:'),
              Text(
                '$_counter',
                style: Theme.of(context).textTheme.display1,
              ),
              new RandomWordsWidget(),
              Row(
                //mainAxisSize: MainAxisSize.max,默认值
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  FlatButton(
                    //添加一个按钮
                    child: new Text('打开新的路由'),
                    textColor: Colors.blue,
                    onPressed: () {
                      Navigator.pushNamed(context, "new_page"); //通过名字打开新路由前提是注册路由表了
                      /*
                  //设置点击
                  Navigator.push(
                      //设置新页面导航
                      context, new MaterialPageRoute(builder: (context) {
                    return new NewRoute();
                  }));*/
                    },
                  ),
                  FlatButton(
                      //添加一个打开状态管理的路由按钮
                      child: new Text('状态管理'),
                      textColor: Colors.blue,
                      onPressed: () {
                        Navigator.push(context,
                            new MaterialPageRoute(builder: (context) {
                          return new StateManagerWidget();
                        }));
                      }),
                  FlatButton(
                      //添加一个打开状态管理的路由按钮
                      child: new Text('Cupertino风格的widget'),
                      textColor: Colors.blue,
                      onPressed: () {
                        Navigator.push(context,
                            new MaterialPageRoute(builder: (context) {
                          return new CupertinoTestRoute();
                        }));
                      }),
                ],
              ),
              Row(
                //mainAxisSize: MainAxisSize.max,默认值为MainAxisSize.max
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  RaisedButton(
                    color: Colors.blue,
                    //按钮背景颜色
                    highlightColor: Colors.blue[700],
                    //按钮按下时的背景颜色
                    colorBrightness: Brightness.dark,
                    //按钮主题，默认是浅色主题
                    splashColor: Colors.grey,
                    //点击时，水波动画中水波的颜色
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20.0)),
                    //外形
                    child: new Text('文本及样式'),
                    //按钮内容
                    textColor: Colors.white,
                    //按钮文字颜色
                    onPressed: () {
                      //按钮点击回调
                      Navigator.push(context,
                          new MaterialPageRoute(builder: (context) {
                        return TextAndTypeRoute();
                      }));
                    },
                  ),
                  RaisedButton(
                      //添加一个打开状态管理的路由按钮
                      child: new Text('单选框和复选框'),
                      textColor: Colors.blue,
                      onPressed: () {
                        Navigator.push(context,
                            new MaterialPageRoute(builder: (context) {
                          return new SwitchAndCheckBoxTestRoute();
                        }));
                      }),
                  RaisedButton(
                      //添加一个打开状态管理的路由按钮
                      child: new Text('输入框和表单'),
                      onPressed: () {
                        Navigator.push(context,
                            new MaterialPageRoute(builder: (context) {
                          return new TextFieldAndFromRoute();
                        }));
                      }),
                ],
              ),
              Wrap(
                spacing: 8.0,
                runSpacing: 4.0,
                alignment: WrapAlignment.center,
                children: <Widget>[
                  OutlineButton(
                    child: new Text('布局类Widgets'),
                    onPressed: () {
                      Navigator.push(context,
                          new MaterialPageRoute(builder: (context) {
                        return new LayoutWidgetTestRoute();
                      }));
                    },
                  ),
                  RaisedButton(
                    color: Colors.blue,
                    highlightColor: Colors.blue[700],
                    colorBrightness: Brightness.dark,
                    splashColor: Colors.grey,
                    //点击时，水波动画中水波的颜色
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20.0)),
                    child: Text('容器类Widgets'),
                    onPressed: () {
                      Navigator.push(context,
                          new MaterialPageRoute(builder: (context) {
                        return new ContainerWidgetsTestRoute();
                      }));
                    },
                  ),
                  RaisedButton(
                    color: Colors.blue,
                    highlightColor: Colors.blue[700],
                    colorBrightness: Brightness.dark,
                    splashColor: Colors.grey,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20.0),
                    ),
                    child: Text('滚动类Widgets'),
                    onPressed: () {
                      Navigator.push(context,
                          new MaterialPageRoute(builder: (context) {
                        return new ScrollWidgetsRoute();
                      }));
                    },
                  ),
                  RaisedButton(
                    color: Colors.blue,
                    highlightColor: Colors.blue[700],
                    colorBrightness: Brightness.dark,
                    splashColor: Colors.grey,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20.0),
                    ),
                    child: Text('功能类Widgets'),
                    onPressed: () {
                      Navigator.push(context,
                          new MaterialPageRoute(builder: (context) {
                        return new FeaturesWidgetsTestRoute();
                      }));
                    },
                  ),
                  RaisedButton(
                    color: Colors.blue,
                    highlightColor: Colors.blue[700],
                    colorBrightness: Brightness.dark,
                    splashColor: Colors.grey,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20.0),
                    ),
                    child: Text('事件处理与通知'),
                    onPressed: () {
                      Navigator.push(context,
                          new MaterialPageRoute(builder: (context) {
                        return new EventHandlingAndNotificationTestRoute();
                      }));
                    },
                  ),
                  RaisedButton(
                    color: Colors.blue,
                    highlightColor: Colors.blue[700],
                    colorBrightness: Brightness.dark,
                    splashColor: Colors.grey,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20.0),
                    ),
                    child: Text('动画'),
                    onPressed: () {
                      Navigator.push(context,
                          new MaterialPageRoute(builder: (context) {
                        return new AnimationsTestRoute();
                      }));
                    },
                  ),
                  RaisedButton(
                    color: Colors.blue,
                    highlightColor: Colors.blue[700],
                    colorBrightness: Brightness.dark,
                    splashColor: Colors.grey,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20.0),
                    ),
                    child: Text('自定义Widgets'),
                    onPressed: () {
                      Navigator.push(context,
                          new MaterialPageRoute(builder: (context) {
                        return new CustomWidgetsTestRoute();
                      }));
                    },
                  ),
                  RaisedButton(
                    color: Colors.blue,
                    highlightColor: Colors.blue[700],
                    colorBrightness: Brightness.dark,
                    splashColor: Colors.grey,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20.0),
                    ),
                    child: Text('文件操作与网络请求'),
                    onPressed: () {
                      Navigator.push(context,
                          new MaterialPageRoute(builder: (context) {
                        return new FileAndNetworkTestRoute();
                      }));
                    },
                  ),
                  RaisedButton(
                    color: Colors.blue,
                    highlightColor: Colors.blue[700],
                    colorBrightness: Brightness.dark,
                    splashColor: Colors.grey,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20.0),
                    ),
                    child: Text('技术胖学习Demo'),
                    onPressed: () {
                      Navigator.push(context,
                          new MaterialPageRoute(builder: (context) {
                        return new JspangStudyDemoRoute();
                      }));
                    },
                  ),
                ],
              )
            ],
          ),
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),
    );
  }

  void _incrementCounter() {
    //设置自增函数
    setState(() {
      _counter++;
    });
  }
}

/*
//官方第一个Demo:ListView+收藏
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
//    final wordPair = new WordPair.random();
    return new MaterialApp(
      title: 'Welcome to Flutter',
      theme: new ThemeData(
        //修改主题
        primaryColor: Colors.blue,
      ),
      home: new RandomWords(),
    );
  }
}

class RandomWords extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new RandomWordsState();
}

class RandomWordsState extends State<RandomWords> {
  final _suggestions = <WordPair>[]; //保存建议单词对,_在Dart中代表私有的

  final _saved = new Set<WordPair>(); //这个集合存储用户喜欢（收藏）的单词对

  final _biggerFont = const TextStyle(fontSize: 18.0); //增大字体的大小

  @override
  Widget build(BuildContext context) {
//    final wordPair=new WordPair.random();
//    return new Text(wordPair.asPascalCase);
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Startup Name Generator'),
        actions: <Widget>[
          new IconButton(icon: new Icon(Icons.list), onPressed: _pushSaved),
        ],
      ),
      body: _buildSuggestions(),
    );
  }

  Widget _buildSuggestions() {
    return new ListView.builder(
        padding: const EdgeInsets.all(16.0),
        // 对于每个建议的单词对都会调用一次itemBuilder，然后将单词对添加到ListTile行中
        // 在偶数行，该函数会为单词对添加一个ListTile row.
        // 在奇数行，该函数会添加一个分割线widget，来分隔相邻的词对。
        // 注意，在小屏幕上，分割线看起来可能比较吃力。
        itemBuilder: (context, i) {
          // 在每一列之前，添加一个1像素高的分隔线widget
          if (i.isOdd) return new Divider();

          // 语法 "i ~/ 2" 表示i除以2，但返回值是整形（向下取整），比如i为：1, 2, 3, 4, 5
          // 时，结果为0, 1, 1, 2, 2， 这可以计算出ListView中减去分隔线后的实际单词对数量
          final index = i ~/ 2;
          // 如果是建议列表中最后一个单词对
          if (index >= _suggestions.length) {
            // ...接着再生成10个单词对，然后添加到建议列表
            _suggestions.addAll(generateWordPairs().take(10));
          }
          return _buildRow(_suggestions[index]);
        });
  }

  Widget _buildRow(WordPair pair) {
    //创建每一行listite
    final alreadySaved = _saved.contains(pair); //检查单词是否在收藏中
    return new ListTile(
      title: new Text(
        pair.asPascalCase,
        style: _biggerFont,
      ),
      trailing: new Icon(
        alreadySaved ? Icons.favorite : Icons.favorite_border,
        color: alreadySaved ? Colors.red : null,
      ),
      onTap: () {
        //对于点击进行响应处理
        setState(() {
          if (alreadySaved) {
            //如果已经收藏的就取消收藏
            _saved.remove(pair);
          } else {
            //未收藏的就收藏
            _saved.add(pair);
          }
        });
      },
    );
  }

  void _pushSaved() {
    //创建一个新的页面
    Navigator.of(context).push(
      new MaterialPageRoute(
        builder: (context) {
          final tiles = _saved.map(
            (pair) {
              return new ListTile(
                title: new Text(
                  pair.asPascalCase,
                  style: _biggerFont,
                ),
              );
            },
          );
          final divided = ListTile.divideTiles(
            context: context,
            tiles: tiles,
          ).toList();
          return new Scaffold(
            appBar: new AppBar(
              title: new Text('Saved Suggestions'),
            ),
            body: new ListView(children: divided),
          );
        },
      ),
    );
  }
}
*/
